package com.leetcode.根据数据结构分类.数组;

import java.util.Arrays;

/**
 * @author: xiaomi
 * @date: 2021/2/17
 * @description: 566. 重塑矩阵
 * https://leetcode-cn.com/problems/reshape-the-matrix/
 */
public class A_566_重塑矩阵 {

    public static void main(String[] args) {
        A_566_重塑矩阵 action = new A_566_重塑矩阵();
        test1(action);
    }

    public static void test1(A_566_重塑矩阵 action) {
        //200
        int r = 2;
        int c = 2;
        int[][] nums = new int[][]{{1, 2, 3, 4}};
        int[][] res = action.matrixReshape(nums, r, c);
        System.out.println("res = " + Arrays.toString(res));
    }

    /**
     * @param nums
     * @param r    行
     * @param c    列
     * @return
     */
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int oldr = nums.length;
        int oldc = nums[0].length;
        int size = oldc * oldr;
        int nSize = r * c;
        if (size != nSize) {
            return nums;
        }
        //此时 可以完全转换
        int[][] res = new int[r][c];
        int row = 0;
        int col = 0;
        for (int i = 0; i < oldr; i++) {
            for (int j = 0; j < oldc; j++) {

                if (col == c) {
                    //此时需要换行
                    row++;
                    col = 0;
                }
                res[row][col++] = nums[i][j];


            }
        }

        return res;
    }
}
